package net.hnjdzy.newsclient;

import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import net.hnjdzy.newsclient.entity.News;
import net.hnjdzy.newsclient.manager.Constants;
import net.hnjdzy.newsclient.manager.NewsManager;
import java.util.List;


public class NewsListFragment extends Fragment {
    public NewsListFragment() {
        super();
    }
    AlertDialog mDialog;
    int mTid;
    public NewsListFragment(int tid) {
        mTid=tid;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        NewsListTask task = new NewsListTask();
        task.execute();
    }
    class  NewsListTask extends AsyncTask<Void,Void,List<News>>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            mDialog =new AlertDialog.Builder(getActivity()).setTitle("提示").setMessage("正在加载...").create();
            mDialog.show();
        }

        @Override
        protected void onPostExecute(List<News> newsList) {
           ListView listView =getView().findViewById(R.id.listview);
           NewsListViewAdapter adapter =new NewsListViewAdapter(newsList,getLayoutInflater());
           listView.setAdapter(adapter);
           mDialog.hide();
       }

        @Override
        protected List<News> doInBackground(Void... voids) {
            NewsManager newsManager =new NewsManager();
            return newsManager.getNewsListByTid(mTid);
        }
    }
    //新闻ListView的适配器
    class NewsListViewAdapter extends BaseAdapter{
        List<News> mNewsList;
        LayoutInflater mInflater;

        public  NewsListViewAdapter(List<News> newsList,LayoutInflater inflater){
            mNewsList =newsList;
            mInflater =inflater;
        }
        @Override
        public int getCount() {
            return mNewsList.size();
        }

        @Override
        public Object getItem(int position) {
            return mNewsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mNewsList.get(position).getNid();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textViewTilte,textViewSummary;
            ImageView imageView;
            View view ;
            if(convertView==null){
                Log.d("test","加载行："+position+",tid:"+mTid);
                view=mInflater.inflate(R.layout.list_item_news,null,false);
                //给界面组件设置内容
                textViewTilte =view.findViewById(R.id.textViewTitle);
                textViewSummary =view.findViewById(R.id.textViewSummary);
                imageView =view.findViewById(R.id.imageView);

                ViewHolder viewHolder =new ViewHolder();
                viewHolder.textViewTitle =textViewTilte;
                viewHolder.textViewSummary=textViewSummary;
                viewHolder.imageView =imageView;

                view.setTag(viewHolder);

            }else {
                ViewHolder viewHolder =(ViewHolder)convertView.getTag();
                textViewTilte =viewHolder.textViewTitle;
                textViewSummary =viewHolder.textViewSummary;
                imageView =viewHolder.imageView;
                view =convertView;
            }
            textViewTilte.setText(mNewsList.get(position).getNtitle());
            textViewSummary.setText(mNewsList.get(position).getNsummary());

            String url = Constants.URL_GET_NEWS_IMAGE +
                    mNewsList.get(position).getNpicpath();
            Picasso.with(getActivity())
                    .load(url)
                    .placeholder(R.drawable.no_image)
                    .into(imageView);
            return view;

        }
    }
    //缓存组件
    class ViewHolder{
        TextView textViewTitle,textViewSummary;
        ImageView imageView;
    }
}